var mysql = require('mysql');
var dbConfig = require('../../config/DBConfig');
var userSQL = require('../../config/Usersql');
var common = require("../common/common");

var pool = mysql.createPool(dbConfig.mysql);
//var connection = mysql.createConnection(dbConfig.mysql);

var responseJSON = function(res, ret) {
	if(typeof ret === 'undefined') {
		res.json({
			code: '-200',
			msg: '操作失败'
		});
	} else {
		res.json(ret);
	}
};

class Sql extends common {

	query_sql(sqlstr) {
		return new Promise((resolve, reject) => {
			pool.getConnection(function(err, connection) {
				connection.query(sqlstr, function(err, result, field) {
					console.log("123456"+result.length);
					if(err)
						reject(result);
					else if(result.length == 0)
						reject(result);
					else
						resolve(result);

					// 释放连接     
					connection.release();
				});
			});
		});
		//connection.connect();
		// 获取前台页面传过来的参数  
		//var param = req.query || req.params;
		// 建立连接 增加一个用户信息 
		//      pool.getConnection(function(err,connection){
		//          connection.query(sqlstr, function(err, result,field) {
		//              //data=JSON.stringify(result);  
		//              //console.log("1");
		//              callback(result);   
		//              /*if (result) {
		//                  result = {
		//                      code: 200,
		//                      msg: '增加成功'
		//                  };
		//              }*/
		//              // 以json形式，把操作结果返回给前台页面     
		//              // responseJSON(res, result);
		//                  
		//              // 释放连接     
		//              connection.release();
		//          });
		//      });
	}
	query_sql(sqlstr,value) {
		console.log("123"+sqlstr+"  "+value);
		return new Promise((resolve, reject) => {
			pool.getConnection(function(err, connection) {
				connection.query(sqlstr,value,function(err, result, field) {
					console.log(result)
					if(err)
						reject(result);
					else if(result.length == 0)
						reject(result);
					else
						resolve(result);

					// 释放连接     
					connection.release();
				});
				
			});
		});
	}

	insert_sql(sqlstr,datalist){
		return new Promise((resolve, reject) => {
			this.query_sql(sqlstr,datalist).then(
                message=>{
                    resolve(message);
                },error=>{
                    reject(error);
                }
            );
		});
	}
	// insert_sql(sqlstr, datalist, callback) {
	// 	pool.getConnection(function(err, connection) {
	// 		connection.query(sqlstr, datalist, function(err, result, field) {
	// 			//data=JSON.stringify(result);  
	// 			console.log("1");
	// 			callback(result);
	// 			/*if (result) {
	// 			    result = {
	// 			        code: 200,
	// 			        msg: '增加成功'
	// 			    };
	// 			}*/
	// 			// 以json形式，把操作结果返回给前台页面     
	// 			// responseJSON(res, result);

	// 			// 释放连接     
	// 			connection.release();
	// 		});
	// 	});
	// }

	delete_sql(sqlstr, datalist, callback) {
		pool.getConnection(function(err, connection) {
			connection.query(sqlstr, datalist, function(err, result, field) {
				//data=JSON.stringify(result);  
				console.log("1");
				callback(result);
				/*if (result) {
				    result = {
				        code: 200,
				        msg: '增加成功'
				    };
				}*/
				// 以json形式，把操作结果返回给前台页面     
				// responseJSON(res, result);

				// 释放连接     
				connection.release();
			});
		});
	}
}

module.exports = Sql;